package cst.cn.edu.neusoft.xumingze224.yuejian_5.Activity;

import android.content.Intent;
import android.content.SharedPreferences;

import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import cst.cn.edu.neusoft.xumingze224.yuejian_5.Bean.LoginBean;
import cst.cn.edu.neusoft.xumingze224.yuejian_5.Listener.TListener;
import cst.cn.edu.neusoft.xumingze224.yuejian_5.Model.LoginModel;
import cst.cn.edu.neusoft.xumingze224.yuejian_5.R;

/**
 * A login screen that offers login via email/password.
 */
public class LoginActivity extends BaseActivity {
    EditText ed_user,ed_password;
    Button btn_login,btn_register;
    String username,userpass;
    String uid;
    private CheckBox chkbox;
    private SharedPreferences sp;//读取记录
    TListener<LoginBean> loginListener=new TListener<LoginBean>() {
        @Override
        public void onResponse(LoginBean loginBean) {
            if(loginBean.getUserid().equals("0")){
                showToast("登陆失败");
            }
            else {
                uid=loginBean.getUserid();

                saveUser();
                Intent intend=new Intent(LoginActivity.this,MainActivity.class);
                showToast(uid);

                startActivity(intend);
            }
        }
        @Override
        public void onFail(String msg) {

        }
    };
    @Override
    void initView() {
        setLayout_filel(R.layout.activity_login);
        ed_password = (EditText) findViewById(R.id.et_password);
        ed_user = (EditText) findViewById(R.id.et_username);
        btn_login = (Button) findViewById(R.id.btn_login);
        btn_register = (Button) findViewById(R.id.btn_register);
        chkbox = (CheckBox) findViewById(R.id.checkbox_remember);
        username = getUser_name();
        userpass = getUser_pass();
        Boolean remember = getUser_remember();
        if (remember == true) {
            ed_user.setText(username);
            ed_password.setText(userpass);
            chkbox.setChecked(remember);
        }
    }
    void initEvent(){
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ed_user.getText().toString().trim().equals("")||ed_password.getText().toString().trim().equals("")){
                    showToast("用户名和密码不能为空");
                }
                else {
                    String tmpuser=ed_user.getText().toString();
                    String tmpass=ed_password.getText().toString();
                    LoginModel loginModel=new LoginModel();
                    loginModel.getLoginList(tmpuser,tmpass,loginListener);

                    //拿着输入的用户名和密码去服务器验证
                    // saveUser();
                    //Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                }
            }
        });
        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intend=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivityForResult(intend,1);
            }
        });
    }
    void initData() {
    }

    public void saveUser(){
        SharedPreferences sp=getSharedPreferences("userInfo",MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("username",ed_user.getText().toString());
        editor.putString("userpass",ed_password.getText().toString());
        editor.putBoolean("remember",chkbox.isChecked());
        editor.putString("user_id",uid);
        editor.commit();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 1:
                if(resultCode==RESULT_OK) {
                    String string=data.getStringExtra("recieve");
                    Toast.makeText(LoginActivity.this,string, Toast.LENGTH_SHORT).show();
                    ed_user.setText(string);
                }
        }
    }
    //loginBean.getUserid().equals("0")
}

